package com.ryan.mdp.callback;

import javax.jms.Message;
import javax.jms.Session;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.remoting.support.RemoteInvocation;

import com.ryan.mdp.json.MdpInvocationResult;
import com.ryan.mdp.service.MdpInvokerServiceExporter;

/**
 * server端接收到异步消息后需要代理callback方法， 把mdpservice中callback调用的arguments传递回client端
 * 
 */
public class MdpCallbackInterceptor implements MethodInterceptor {

    private final Message requestMessage;
    private final Session session;
    private final RemoteInvocation invocation;
    private final MdpInvokerServiceExporter serviceExporter;

    public MdpCallbackInterceptor(Message requestMessage, Session session, RemoteInvocation invocation,
                    MdpInvokerServiceExporter serviceExporter) {
        this.requestMessage = requestMessage;
        this.session = session;
        this.invocation = invocation;
        this.serviceExporter = serviceExporter;
    }

    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object[] args = invocation.getArguments();
        serviceExporter.writeRemoteInvocationResult(requestMessage, session, this.invocation,
                        new MdpInvocationResult(args[0]));
        return null;
    }
}
